package com.ruoyi.web.controller.userActivity;

import com.ruoyi.activity.domain.Activity;
import com.ruoyi.activity.service.IActivityService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.UserActivity;
import com.ruoyi.system.service.IUserActivityService;
import org.apache.poi.ss.formula.functions.Now;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户优惠卡管理Controller
 * 
 * @author ruoyi
 * @date 2025-06-02
 */
@RestController
@RequestMapping("/userActivity/userActivity")
public class UserActivityController extends BaseController
{
    @Autowired
    private IUserActivityService userActivityService;
    @Autowired
    private IActivityService activityService;
    /**
     * 查询用户优惠卡管理列表
     */
    @PreAuthorize("@ss.hasPermi('userActivity:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserActivity userActivity)
    {
        startPage();
        List<UserActivity> list = userActivityService.selectUserActivityList(userActivity);
        return getDataTable(list);
    }

    /**
     * 导出用户优惠卡管理列表
     */
    @PreAuthorize("@ss.hasPermi('userActivity:activity:export')")
    @Log(title = "用户优惠卡管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserActivity userActivity)
    {
        List<UserActivity> list = userActivityService.selectUserActivityList(userActivity);
        ExcelUtil<UserActivity> util = new ExcelUtil<UserActivity>(UserActivity.class);
        util.exportExcel(response, list, "用户优惠卡管理数据");
    }

    /**
     * 获取用户优惠卡管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('userActivity:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userActivityService.selectUserActivityById(id));
    }

    /**
     * 新增用户优惠卡管理
     */
    @PreAuthorize("@ss.hasPermi('userActivity:activity:add')")
    @Log(title = "用户优惠卡管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserActivity userActivity)
    {
        return toAjax(userActivityService.insertUserActivity(userActivity));
    }

    /**
     * 修改用户优惠卡管理
     */
    @PreAuthorize("@ss.hasPermi('userActivity:activity:edit')")
    @Log(title = "用户优惠卡管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserActivity userActivity)
    {
        return toAjax(userActivityService.updateUserActivity(userActivity));
    }

    /**
     * 删除用户优惠卡管理
     */
    @PreAuthorize("@ss.hasPermi('userActivity:activity:remove')")
    @Log(title = "用户优惠卡管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userActivityService.deleteUserActivityByIds(ids));
    }
//    根据用户获取会员卡,并筛选适合的优惠
    @GetMapping("/getActivitiesByUser")
    public TableDataInfo getgetActivitiesByUser(@RequestParam Long gameTypeId){
        Long userId = SecurityUtils.getUserId();
        UserActivity userActivity = new UserActivity();
        userActivity.setUserId(userId);
        ArrayList<Activity> list = new ArrayList<>();
        List<UserActivity> userActivities = userActivityService.selectUserActivityList(userActivity);
        for (UserActivity activity : userActivities) {
            if (activity.getIsUsed() == 0 &&
                    activity.getExpireTime().toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime()
                            .isAfter(LocalDateTime.now())){
                Long activityId = activity.getActivityId();
                Activity activity1 = activityService.selectActivityById(activityId);
                if (activity1.getGameTypeId()==gameTypeId){
                    list.add(activity1);
                }
            }
        }
        return getDataTable(list);
    }
}
